# 强调python中没有switch语句
"""
其他语言中switch语法：
switch(val) {
    case value1:
        do_some_opr();
        break;
    case value2:
        do_some_opr();
        break;
    ...
    case valuen:
        do_some_opr();
        break;
    default:
        do_default_opr()
}
"""
# python使用字典完成switch功能

def jia(x,y):
    return x+y

def jian(x,y):
    return x - y

def cheng(x,y):
    return x*y

def chu(x,y):
    return x / y

opr = {
    '+':jia,
    '-':jian,
    '*':cheng,
    '/':chu
}

def f(x,o,y):
    return opr.get(o)(x,y)

res = f(3,'+',2)
print(res)

# 使用lambda来实现switch功能
res1 = {
    'a':lambda x:x*5,
    'b':lambda x:x+5,
    'c':lambda x:x-5
} 

result = res1['a'](3)
print(result)

# 类实现switch功能
class Switch():
    def __init__(self,value):
        self.value = value
        self.fall = False

    def __iter__(self):
        yield self.match
        raise StopIteration

    def match(self,*args):
        if self.fall or not args:
            return True
        elif self.value in args:
            self.fall = True
            return True
        else:
            return False

v = 'ten'
for case in Switch(v):
    if case('one'):
        print(1)
        break
    if case('two'):
        print(2)
        break
    if case('ten'):
        print(10)
        break
    if case():
        print("退出")
        exit(1)